package com.tz.dao;

import com.tz.entity.Category;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
@Repository
@Transactional
public class CategoryDaoImpl implements CategoryDao{

    @Autowired
    private SessionFactory sf;

    private Session getSession(){
        return sf.getCurrentSession();
    }
    @Override
    public List<Category> findCategoryByCategoryId(long parentId) {
        List<Category> categories = getSession().createQuery("from Category where parent=:parentId").setLong("parentId", parentId).list();
        for(Category c : categories){
            c.getChildren().size();
        }
        return categories;
    }

    @Override
    public Category findCategoryById(long categoryId) {
        return (Category) getSession().get(Category.class,categoryId);
    }
}
